///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_IO_CFILEFILTER_H__
#define __BUOLA_IO_CFILEFILTER_H__

#include <buola/buola.h>

namespace buola { namespace io {

///\addtogroup filesystem
///@{

/// file filter base class

class CFileFilter : public UShared
{
public:
    explicit CFileFilter(const std::string &pName=std::string())
        :mName(pName)
    {
    }

    virtual ~CFileFilter()=0;

    const std::string &GetName()            {   return mName;   }

    virtual bool CheckFile(const CFile &pFile);
    virtual bool CheckURI(const CURI &pURI);
    virtual bool CheckName(const std::string &pName);

protected:
    std::string mName;
};

/// filters files which match certain flags

class CFlagsFileFilter : public CFileFilter
{
public:
    enum EFlags
    {
        FLAG_NONE=0,
        NOFILES=(1<<0),
        NOFOLDERS=(1<<1),
        HIDDEN=(1<<2),
        RELATIVES=(1<<3),
        BROKENLINKS=(1<<4)
    };
    DECLARE_FRIEND_ENUM_BITOPS(EFlags)

    explicit CFlagsFileFilter(EFlags pFlags,
                    const std::string &pName=std::string())
        :CFileFilter(pName),mFlags(pFlags)
    {
    }

    virtual ~CFlagsFileFilter();

    void SetFlags(EFlags pFlags)        {   mFlags=pFlags;  }

    bool CheckFile(const CFile &pFile);
    bool CheckName(const std::string &pName);

protected:
    EFlags mFlags;
};

/// filters files based on regular expressions

class CRegexFileFilter : public CFileFilter
{
public:
    enum
    {
        NOFILES=(1<<0),
        NOFOLDERS=(1<<1),
        HIDDEN=(1<<2),
        RELATIVES=(1<<3)
    };

    explicit CRegexFileFilter(const std::string &pRegExp,
                            const std::string &pName=std::string());
    virtual ~CRegexFileFilter();

    void AddRegExp(const std::string &pRegExp);

    bool CheckName(const std::string &pName);

protected:
    std::vector<std::string> mRegExps;
};

/// or operator for file filters

class COrFileFilter : public CFileFilter
{
public:
    COrFileFilter(PFileFilter p1,PFileFilter p2,
                    const std::string &pName=std::string());

    bool CheckFile(const CFile &pFile);
    bool CheckURI(const CURI &pURI);
    bool CheckName(const std::string &pName);

protected:
    PFileFilter m1;
    PFileFilter m2;
};

/// and operator for file filters

class CAndFileFilter : public CFileFilter
{
public:
    CAndFileFilter(PFileFilter p1,PFileFilter p2,
                    const std::string &pString=std::string());

    bool CheckFile(const CFile &pFile);
    bool CheckURI(const CURI &pURI);
    bool CheckName(const std::string &pName);

protected:
    PFileFilter m1;
    PFileFilter m2;
};

///@}

/*namespace io*/ } /*namespace buola*/ }

#endif
